package org.springboot.szsrcb.controller;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.szsrcb.entity.Attachment;
import org.springboot.szsrcb.entity.Req_base_info;
import org.springboot.szsrcb.entity.Req_bill;
import org.springboot.szsrcb.entity.Req_work_log;
import org.springboot.szsrcb.entity.User;
import org.springboot.szsrcb.srvice.Att_Service;
import org.springboot.szsrcb.srvice.Req_bill_Service;
import org.springboot.szsrcb.srvice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/req")
public class ReqController {

	private static final Logger logger = LoggerFactory.getLogger(ReqController.class);

	@Autowired
	private Req_bill_Service req_bill_Service;

	@Autowired
	private UserService userService;

	@Autowired
	private Att_Service att_Service;

	public static String parseGBK(String sIn) {
		if (sIn == null || sIn.equals(""))
			return sIn;
		try {
			return new String(sIn.getBytes("GBK"), "ISO-8859-1");
		} catch (UnsupportedEncodingException usex) {
			return sIn;
		}
	}

	@RequestMapping("/req_assign_detail_page_download_att")
	public boolean req_assign_detail_page_download_att(HttpServletRequest request, HttpServletResponse response) {
		boolean result = false;
		String get_att_id = (String) request.getParameter("att_id");
		Attachment att = att_Service.search_a_att_by_id(get_att_id);
		// response.reset();
		// response.setContentType("multipart/form-data");
		String iso_filename = parseGBK(att.getAtt_file_name());

		response.setHeader("Content-Disposition", "attachment; filename=" + iso_filename);
		response.setContentType("application/x-msdownload;charset=utf-8");

		// response.setContentType("application/octet-stream; charset=utf-8");
		// 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
		try {
			// response.flushBuffer();
			Blob blob = att.getAtt_data();
			// String filetype=(String) list.get(1);
			int length = (int) blob.length();
			byte[] bImage = new byte[length];
			InputStream is = new BufferedInputStream(blob.getBinaryStream());
			is.read(bImage, 0, length);
			OutputStream out = response.getOutputStream();
			out.write(bImage);
			out.flush();
			out.close();
			is.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		result = true;
		return result;
	}

	@RequestMapping(value = "/req_new", method = { RequestMethod.GET })
	public String req_new(HttpServletRequest request, Model model, HttpSession httpSession) {
		String login_name = (String) httpSession.getAttribute("login_name");
		if (login_name != null) {
			logger.info("access /req/req_new ");
			return "req_new";
		} else {
			logger.info("access login page.");
			return "/login";
		}
	}

	@RequestMapping(value = "/req_assign_detail", method = { RequestMethod.GET })
	public String req_assign_detail(HttpServletRequest request, Model model, HttpSession httpSession) {
		String login_name = (String) httpSession.getAttribute("login_name");
		if (login_name != null) {
			logger.info("access /req/req_assign_detail ");
			String g_p = request.getParameter("params");
			Req_base_info m = new Req_base_info();
			model.addAttribute("test_v", g_p);

			// model.addAttribute("name", name);
			return "req_assign_detail";
		} else {
			logger.info("access login page.");
			return "/login";
		}
	}

	@RequestMapping(value = "/req_modify_detail", method = { RequestMethod.GET })
	public String req_modify_detail(HttpServletRequest request, Model model, HttpSession httpSession) {
		String login_name = (String) httpSession.getAttribute("login_name");
	
		logger.info("access /req/req_modify_detail ");
		String req_id = request.getParameter("req_id");
		// Req_base_info m=new Req_base_info();
		model.addAttribute("test_v", req_id);
		
		User u = userService.search_a_user(login_name);
		if (u != null) {

			// String role=u.getRole();
			Req_bill req = req_bill_Service.req_bill_search_by_id(req_id);
			String g_status = req.getReq_status();
			/*
			 * String check_role1="部门主管领导"; String check_role2="项目管理中心负责人";
			 * String check_role3="业务方案中心负责人";
			 */
			String check_value1 = "已接收";
			String check_value2 = "已退回";
			boolean is_leader = userService.is_leader(u.getName());

			/*if (is_leader) {
				model.addAttribute("is_base_info", "true");

				if (g_status.equals(check_value1) || g_status.equals(check_value2)) {
					model.addAttribute("is_team_info", "false");
					model.addAttribute("is_workload_info", "false");
					model.addAttribute("is_work_log_info", "false");
				} else {
					model.addAttribute("is_team_info", "true");
					model.addAttribute("is_workload_info", "true");
					model.addAttribute("is_work_log_info", "true");
				}
			} else {
				model.addAttribute("is_base_info", "false");
				model.addAttribute("is_team_info", "false");
				if (g_status.equals(check_value1) || g_status.equals(check_value2)) {
					model.addAttribute("is_team_info", "false");
					model.addAttribute("is_workload_info", "false");
					model.addAttribute("is_work_log_info", "false");
				} else {
					boolean is_req_head = req_bill_Service.check_is_req_head(u.getName(), req_id);
					if (is_req_head) {
						model.addAttribute("is_base_info", "true");
						model.addAttribute("is_workload_info", "true");
					} else {
						model.addAttribute("is_workload_info", "false");
					}
					model.addAttribute("is_work_log_info", "true");
				}

			}*/
			    if(g_status!=null){
			    	model.addAttribute("is_base_info", "true");
					if (g_status.equals(check_value1) || g_status.equals(check_value2)) {
						model.addAttribute("is_team_info", "false");
						model.addAttribute("is_workload_info", "false");
						model.addAttribute("is_work_log_info", "false");
					} else {
						model.addAttribute("is_team_info", "true");
						model.addAttribute("is_workload_info", "true");
						model.addAttribute("is_work_log_info", "true");
					}
			    }else{
			    	model.addAttribute("is_team_info", "false");
					model.addAttribute("is_workload_info", "false");
					model.addAttribute("is_work_log_info", "false");
			    }
			// model.addAttribute("name", name);
			return "req_modify_detail";
		} else {
			return "login";
		}
	}

	// @RequestMapping("/req_reject")
	@RequestMapping(value = "/req_reject")
	public String req_reject(HttpServletRequest request, Model model) {
		// logger.info("req_reject");
		logger.info("access /req/req_reject ");
		String req_id = request.getParameter("req_id");
		// logger.info("req_id = "+req_id);
		model.addAttribute("req_id", req_id);
		return "req_reject";
	}

	@RequestMapping("/req_modify_detail_base_info")
	public String req_modify_detail_base_info(HttpServletRequest request, Model model, HttpSession httpSession) {
		logger.info("access /req/req_modify_detail_base_info");
		String req_id = request.getParameter("req_id");
		// logger.info("req_id = "+req_id);
		model.addAttribute("req_id", req_id);
		model.addAttribute("in_act_work_load", req_id);
		String in_req_status = req_bill_Service.get_req_status(req_id);
		model.addAttribute("in_req_status", in_req_status);

		String login_name = (String) httpSession.getAttribute("login_name");
		User u = userService.search_a_user(login_name);
		boolean is_leader = userService.is_leader(u.getName());
		
		/*boolean is_req_head = req_bill_Service.check_is_req_head(u.getName(), req_id);
		if (is_req_head) {
			is_leader=true;
		}*/
		model.addAttribute("is_leader", is_leader);
		// model.addAttribute("name", name);
		return "req_modify_detail_base_info";
	}

	@RequestMapping("/req_modify_detail_team_info")
	public String req_modify_detail_team_info(HttpServletRequest request, Model model) {
		logger.info("access /req/req_modify_detail_team_info");
		String req_id = request.getParameter("req_id");
		model.addAttribute("req_id", req_id);
		// model.addAttribute("name", name);
		return "req_modify_detail_team_info";
	}

	@RequestMapping("/req_modify_detail_workload_info")
	public String req_modify_detail_workload_info(HttpServletRequest request, Model model) {
		logger.info("access /req/req_modify_detail_team_info");
		String req_id = request.getParameter("req_id");
		model.addAttribute("req_id", req_id);
		// String
		// tmp="/req/req_modify_detail_workload_info_save?req_id="+req_id;
		// model.addAttribute("action", tmp);

		// model.addAttribute("name", name);
	
		
		return "req_modify_detail_workload_info";
	}

	@RequestMapping("/req_modify_detail_work_log_info")
	public String req_modify_detail_work_log_info(HttpServletRequest request, Model model) {
		logger.info("access /req/req_modify_detail_work_log_info");
		String req_id = request.getParameter("req_id");
		model.addAttribute("req_id", req_id);
		// model.addAttribute("name", name);
		return "req_modify_detail_work_log_info";
	}

}
